﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace FrameworkXNA
{
    class AnimatedObject : Object2D
    {
        public struct AnimationStructure
        {
            public string name;
            public Vector2 frameSize;
            public Vector2 sheetPosition;
            public int numFrames;
            public int frameDuration;
            public LoopType loop;
            public bool isFinished;
        }

        public enum LoopType
        {
            InfiniteLoop,
            LoopOnce,
            FixedLastFrame,
        }

        Texture2D spriteSheet;

        Rectangle destination;

        public AnimationStructure currentAnimation;

        Dictionary<string, AnimationStructure> animationList = new Dictionary<string, AnimationStructure>();

        int timeCounter = 0;
        public int currentFrame = 0;

        public AnimatedObject(Texture2D spriteSheet):base(spriteSheet)
        {
            this.spriteSheet = spriteSheet;
        }

        public void AddAnimation(string animationName, int frameWidth, int frameHeight, 
            int numFrames, int frameDuration, LoopType loop, int frameCoordX = 0, 
            int frameCoordY = 0)
        {
            AnimationStructure newAnim = new AnimationStructure();
            newAnim.name = animationName;
            newAnim.loop = loop;
            newAnim.numFrames = numFrames;
            newAnim.frameSize = new Vector2(frameWidth, frameHeight);
            newAnim.sheetPosition = new Vector2(frameCoordX, frameCoordY);
            newAnim.frameDuration = frameDuration;
            animationList.Add(animationName, newAnim);
        }

        public void setAnimation(string animationName)
        {
            if (currentAnimation.name != animationName)
            {
                currentAnimation = animationList[animationName];
                timeCounter = 0;
            }
        }

        public override void Update()
        {
            if (currentAnimation.isFinished)
            {
                return;
            }

            timeCounter++;
            if (timeCounter >= currentAnimation.numFrames * currentAnimation.frameDuration)
            {
                if (currentAnimation.loop != LoopType.InfiniteLoop)
                {
                    currentAnimation.isFinished = true;
                }

                if (currentAnimation.loop == LoopType.FixedLastFrame)
                {
                    return;
                }

                timeCounter = 0;
            }

            currentFrame = timeCounter / currentAnimation.frameDuration;
        }

        public override void CalcularRetangulo()
        {
            collisionBounds = destination;
            
        }

        public override void Draw(SpriteBatch spritebatch)
        {
            if ((currentAnimation.isFinished) && (currentAnimation.loop == LoopType.LoopOnce))
            {
                return;
            }

            Rectangle source;

            source = new Rectangle((int)(currentAnimation.sheetPosition.X + currentFrame 
                * currentAnimation.frameSize.X),
                                   (int)currentAnimation.sheetPosition.Y,
                                   (int)currentAnimation.frameSize.X,
                                   (int)currentAnimation.frameSize.Y);

             destination = new Rectangle((int)x,
                                         (int)y,
                                         (int)(currentAnimation.frameSize.X * scaleX),
                                         (int)(currentAnimation.frameSize.Y * scaleY));

            spritebatch.Draw(spriteSheet, destination, source, color * alpha, rotation, 
                Vector2.Zero, spriteEffects, 1f);
        }
    }
}
